﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class Staff
    {
        public int Add(Chain.Model.Staff model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Staff(");
            builder.Append("StaffNumber,StaffName,StaffSex,StaffMobile,StaffAddress,StaffClassID,StaffRemark)");
            builder.Append(" values (");
            builder.Append("@StaffNumber,@StaffName,@StaffSex,@StaffMobile,@StaffAddress,@StaffClassID,@StaffRemark)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffNumber", SqlDbType.VarChar, 50), new SqlParameter("@StaffName", SqlDbType.NVarChar, 50), new SqlParameter("@StaffSex", SqlDbType.Bit, 1), new SqlParameter("@StaffMobile", SqlDbType.NVarChar, 50), new SqlParameter("@StaffAddress", SqlDbType.NVarChar, 500), new SqlParameter("@StaffClassID", SqlDbType.Int, 4), new SqlParameter("@StaffRemark", SqlDbType.NVarChar, 500) };
            cmdParms[0].Value = model.StaffNumber;
            cmdParms[1].Value = model.StaffName;
            cmdParms[2].Value = model.StaffSex;
            cmdParms[3].Value = model.StaffMobile;
            cmdParms[4].Value = model.StaffAddress;
            cmdParms[5].Value = model.StaffClassID;
            cmdParms[6].Value = model.StaffRemark;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int StaffID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Staff ");
            builder.Append(" where StaffID=@StaffID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StaffID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string StaffIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Staff ");
            builder.Append(" where StaffID in (" + StaffIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int StaffID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from Staff");
            builder.Append(" where StaffID=@StaffID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StaffID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool Exists(int intStaffID, string strStaffNumber)
        {
            string format = "select StaffID,StaffNumber from Staff where StaffID not in ({0}) and (StaffNumber = '{1}')";
            DataTable table = DbHelperSQL.Query(string.Format(format, intStaffID, strStaffNumber)).Tables[0];
            return (table.Rows.Count > 0);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select StaffID,StaffNumber,StaffName,StaffSex,StaffMobile,StaffAddress,StaffClassID,ClassName,ClassPercent,ShopName,StaffRemark ");
            builder.Append(" FROM Staff,SysShop,StaffClass ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" StaffID,StaffNumber,StaffName,StaffSex,StaffMobile,StaffAddress,StaffClassID,StaffRemark ");
            builder.Append(" FROM Staff ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.StaffID desc");
            }
            builder.Append(")AS Row, T.*  from Staff T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, string strTime, params string[] strWhere)
        {
            string tableName = "Staff,StaffClass,SysShop";
            string[] column = new string[] { "Staff.*,StaffClass.ClassName,SysShop.ShopName,SysShop.ShopID,(select ISNULL(sum(StaffTotalMoney),0) from StaffMoney where Staff.StaffID=StaffMoney.StaffID and " + strTime + ") as TotalMoney" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "ShopName", "StaffID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("StaffID", "Staff");
        }

        public Chain.Model.Staff GetModel(int StaffID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 StaffID,StaffNumber,StaffName,StaffSex,StaffMobile,StaffAddress,StaffClassID,StaffRemark from Staff ");
            builder.Append(" where StaffID=@StaffID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffID", SqlDbType.Int, 4) };
            cmdParms[0].Value = StaffID;
            Chain.Model.Staff staff = new Chain.Model.Staff();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["StaffID"] != null) && (set.Tables[0].Rows[0]["StaffID"].ToString() != ""))
            {
                staff.StaffID = int.Parse(set.Tables[0].Rows[0]["StaffID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffNumber"] != null) && (set.Tables[0].Rows[0]["StaffNumber"].ToString() != ""))
            {
                staff.StaffNumber = set.Tables[0].Rows[0]["StaffNumber"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StaffName"] != null) && (set.Tables[0].Rows[0]["StaffName"].ToString() != ""))
            {
                staff.StaffName = set.Tables[0].Rows[0]["StaffName"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StaffSex"] != null) && (set.Tables[0].Rows[0]["StaffSex"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["StaffSex"].ToString() == "1") || (set.Tables[0].Rows[0]["StaffSex"].ToString().ToLower() == "true"))
                {
                    staff.StaffSex = true;
                }
                else
                {
                    staff.StaffSex = false;
                }
            }
            if ((set.Tables[0].Rows[0]["StaffMobile"] != null) && (set.Tables[0].Rows[0]["StaffMobile"].ToString() != ""))
            {
                staff.StaffMobile = set.Tables[0].Rows[0]["StaffMobile"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StaffAddress"] != null) && (set.Tables[0].Rows[0]["StaffAddress"].ToString() != ""))
            {
                staff.StaffAddress = set.Tables[0].Rows[0]["StaffAddress"].ToString();
            }
            if ((set.Tables[0].Rows[0]["StaffClassID"] != null) && (set.Tables[0].Rows[0]["StaffClassID"].ToString() != ""))
            {
                staff.StaffClassID = int.Parse(set.Tables[0].Rows[0]["StaffClassID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StaffRemark"] != null) && (set.Tables[0].Rows[0]["StaffRemark"].ToString() != ""))
            {
                staff.StaffRemark = set.Tables[0].Rows[0]["StaffRemark"].ToString();
            }
            return staff;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM Staff ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int Update(Chain.Model.Staff model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Staff set ");
            builder.Append("StaffNumber=@StaffNumber,");
            builder.Append("StaffName=@StaffName,");
            builder.Append("StaffSex=@StaffSex,");
            builder.Append("StaffMobile=@StaffMobile,");
            builder.Append("StaffAddress=@StaffAddress,");
            builder.Append("StaffClassID=@StaffClassID,");
            builder.Append("StaffRemark=@StaffRemark");
            builder.Append(" where StaffID=@StaffID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StaffNumber", SqlDbType.VarChar, 50), new SqlParameter("@StaffName", SqlDbType.NVarChar, 50), new SqlParameter("@StaffSex", SqlDbType.Bit, 1), new SqlParameter("@StaffMobile", SqlDbType.NVarChar, 50), new SqlParameter("@StaffAddress", SqlDbType.NVarChar, 500), new SqlParameter("@StaffClassID", SqlDbType.Int, 4), new SqlParameter("@StaffRemark", SqlDbType.NVarChar, 500), new SqlParameter("@StaffID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.StaffNumber;
            cmdParms[1].Value = model.StaffName;
            cmdParms[2].Value = model.StaffSex;
            cmdParms[3].Value = model.StaffMobile;
            cmdParms[4].Value = model.StaffAddress;
            cmdParms[5].Value = model.StaffClassID;
            cmdParms[6].Value = model.StaffRemark;
            cmdParms[7].Value = model.StaffID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }
    }
}

